﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TimeTableManagement.Entities;
using System.Web.Script.Serialization;

namespace TimeTableManagement.Controllers
{
    [Authorize]
    public class ConfigController : Controller
    {
        QLSVContext db = new QLSVContext();
        //
        // GET: /Config/

        public ActionResult Index()
        {
            //if (!checkPermission("view")) return RedirectToAction("Index", "Home");
            var model = db.Tb_Log.ToList();
            return View(model);
        }
        public ActionResult SystemLog()
        {
            var model = db.Tb_Log.Where(g => g.option.Equals("system"));
            return View(model);
        }
        public ActionResult UserLog()
        {
            var model = db.Tb_Log.Where(g => g.option.Equals("user"));
            return View(model);
        }
        public ActionResult DbLog()
        {
            var model = db.Tb_Log.Where(g => g.option.Equals("database"));
            return View(model);
        }
        [HttpPost]
        public ActionResult Create(Tb_Config tb_config, String control, String action)
        {
            if (ModelState.IsValid)
            {
                tb_config.keyword = TimeTableManagement.Models.Functions.RandomPassword(10);
                db.Tb_Config.Add(tb_config);
                db.SaveChanges();
                return RedirectToAction(action,control);
            }

            return View(tb_config);
        }
        public ActionResult InforSchool()
        {
            
            return View();
        }
        [HttpPost]
        public ActionResult InforSchool(String inforschool)
        {
            if (ModelState.IsValid)
            {
                JavaScriptSerializer jss = new JavaScriptSerializer();
                List<InforSchool> list = jss.Deserialize<List<InforSchool>>(inforschool);
                foreach (InforSchool _inforS in list)
                {
                    var InFO = db.Tb_Config.Find(_inforS.id);
                    InFO.value = _inforS.value;
                    db.SaveChanges();
                }
                return RedirectToAction("Index");
            }
            return View();
        }
    }
    public class InforSchool
    {
        int _id;
        String _key;
        String _value;
        public int id
        {
            get { return _id; }
            set { _id = value; }
        }
        public String key
        {
            get { return _key; }
            set { _key = value; }
        }
        public String value
        {
            get { return _value; }
            set { _value = value; }
        }
    }
}
